/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chat.sound;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;

/**
 *
 * @author salm
 */
public class SoundPlayer implements SoundRecorderListener
{
    private SourceDataLine sourceDataLine;
    private AudioFormat audioFormat;
    
    public SoundPlayer()
    {
        this.initAudioFormat();
        this.initDataLine();
    }
    
    private void initDataLine() {
        try {
            DataLine.Info dataLineInfo = new DataLine.Info(
                    SourceDataLine.class, audioFormat);
            sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);
            sourceDataLine.open(audioFormat);
            sourceDataLine.start();
        } catch (Exception e) {
            System.out.println(e);
            System.exit(0);
        }
    }

    private void initAudioFormat() {
        float sampleRate = 8000.0F;
        int sampleSizeInBits = 16;
        int channels = 1;
        boolean signed = true;
        boolean bigEndian = false;
        this.audioFormat = new AudioFormat(
                sampleRate,
                sampleSizeInBits,
                channels,
                signed,
                bigEndian);
    }

    @Override
    public synchronized void onRecordData(byte[] recordData, int recordLen)
    {
        this.sourceDataLine.write(recordData, 0, recordLen);
    }
}